package university;
import university.exceptions.*;

import java.util.*;

/**
 * Class that describes retry of test
 * @author anatolii.bondarenko
 */

class RetryTest extends TakeATest {
    private int numberOfRetries;
    private List<Student> studentsForRetry;
    /**
     * Details of test retry
     * @param d is discipline of test(teacher is taken from discipline class)
     * @param sfr is list of students for retry of test
     * @param ev is time and place of test retry
     * @param retr
     */
    public RetryTest(Discipline d, List<Student> sfr, ExamEvent ev, int retr) throws ListEmptyException, InvalidNumberOfRetriesException{
		super(d,ev);
		numberOfRetries = retr;
		studentsForRetry = sfr;
        if (sfr.size() == 0){
            throw new ListEmptyException("No Students for this retry");
        }
        if (retr < 1 || retr > 3){
            throw new InvalidNumberOfRetriesException("Retry number can be from 1 to 3");
        }
	}

    /**
     * Signs that student passed exam retry and reduces number of failed disciplines.
     * Also Student is removed from list of students that failed this test
     * @param s
     */
    public void pass(Student s) throws NoElementInListException, ExamineResignedStudentException, NoFailedDisciplinesException{
        if (!studentsForRetry.contains(s)){
            throw new NoElementInListException("No such student for retry");
        }
        if (s.isResigned()){
            throw new ExamineResignedStudentException("Cannot examine resigned students");
        }
        s.reduceFailedDisciplines();
		studentsForRetry.remove(s);
	}

    /**
     * returns the list of students that failed the test
     * @return list of students  that failed the test
     */
    public List<Student> getStudentsForRetry(){
        return studentsForRetry;
    }

    /**
     * Gets details of test retry in String format
     * @return details of test retry
     */
    public String toString(){
        String s = "Test retry for discipline" + super.getDiscipline() + super.getTimeAndPlace() + ". Teacher: "
                + super.getTeacher() + ". For Students: ";
        for(Student st:studentsForRetry){
            s = s + st.getName()+", ";
        }
        s = s.trim();
        s = s + ".";
        return s;
    }

    /**
     * If students fails the test he ramains in list of students that failed this test.
     * If number of retries is larger that 3 student is resigned.
     * @param s
     */
    public void fail(Student s)throws NoElementInListException, ExamineResignedStudentException{
        if (!studentsForRetry.contains(s)){
            throw new NoElementInListException("No such student for retry");
        }
        if (s.isResigned()){
            throw new ExamineResignedStudentException("Cannot examine resigned students");
        }
		if (numberOfRetries>=3){
			s.resign();
		}
	}

}
